package com.avocado.base.controller;

import com.avocado.base.dto.user.*;
import com.avocado.base.entity.User;
import com.avocado.base.entity.UserRole;
import com.avocado.base.service.UserService;
import com.avocado.base.vo.UserInfo;
import com.avocado.commons.model.http.BaseResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.util.ArrayList;


@Controller
@RequestMapping("/user/*")
public class UserController {

	private  final UserService userService;

	public UserController(UserService userService) {
		this.userService = userService;
	}

	 @RequestMapping("/getOne")
     public @ResponseBody BaseResponse getOne(Long id) {

		 return BaseResponse.builder().status(1).data(userService.getOne(id)).build();
     }

	 @PreAuthorize("hasAnyAuthority('USER_ADD')")
	 @RequestMapping("/add")
     public @ResponseBody BaseResponse add(@RequestBody @Validated UserAddDto dto) throws Exception {

		 long id=userService.add(dto);

		 return BaseResponse.builder().status(1).data(id).message("操作成功!").build();
	 }


	 @PreAuthorize("hasAnyAuthority('USER_UPDATE')")
	 @RequestMapping("/update")
	 //清空用户菜单缓存
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse update(@RequestBody @Validated UserEditDto dto) throws Exception {

		 userService.update(dto);

		 return BaseResponse.builder().status(1).message("操作成功!").build();
     }

	 @PreAuthorize("hasAnyAuthority('USER_QUERY')")
	 @RequestMapping("/findByPage")
     public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable, UserQueryDto dto) {

		 return BaseResponse.builder().status(1).data(userService.findByPage(pageable, dto)).build();
     }



	 @PreAuthorize("hasAnyAuthority('USER_DELETE')")
	 @RequestMapping("/deleteById")
	 //清空用户菜单缓存
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse deleteById(Long id) {

		 int status=userService.deleteById(id)==Boolean.TRUE?1:0;

		 return BaseResponse.builder().status(status).message("操作成功!").build();
     }




	@RequestMapping("/getLoginUser")
	public @ResponseBody BaseResponse getLoginUser(Principal user) {

		UserInfo userInfo= UserInfo.builder().roleNameList(new ArrayList<>()).build();

		User dbUser=userService.findByName(user.getName());

		BeanUtils.copyProperties(dbUser,userInfo);

		for(UserRole userRole: dbUser.getUserRoles()){

			userInfo.getRoleNameList().add(userRole.getRole().getName());
		}

		return BaseResponse.builder().status(1).data(userInfo).build();
	}


	@RequestMapping("/updateProfile")
	public @ResponseBody BaseResponse updateProfile(@RequestBody @Validated UserProfileDto dto, Principal user){

		userService.updateProfile(dto,user.getName());

		return BaseResponse.builder().status(1).message("操作成功!").build();
	}


	@RequestMapping("/updatePhoto")
	public @ResponseBody BaseResponse updatePhoto(Principal user,String photo){

		 userService.updatePhoto(photo,user.getName());

		return BaseResponse.builder().status(1).message("操作成功!").build();
	}


	@RequestMapping("/findAllByRoleCode")
	public @ResponseBody BaseResponse findAllByRoleCode(String code) {

		return BaseResponse.builder().status(1).data(userService.findAllByRoleCode(code)).build();
	}

}
